<?php
session_start();
include("../db_connect.php");

if(isset($_SESSION['cart']))
{
	if(is_array($_SESSION['cart'])){
		
		$max = count($_SESSION['cart']);
		if($max>0){
			$display = '<table width="200" border="1">	<tr><th>Name</th><th>Price</th><th>Qty</th><th>x</th></tr>';
			for($i=0; $i<$max; $i++){
				$pid = $_SESSION['cart'][$i]['productid'];
				$qty = $_SESSION['cart'][$i]['qty'];
				//echo $pid . "-" . $qty ."<>";
				$display .= displaycart($pid,$qty);
			
			}
			$display .="</tr> </table>";
			$display .= '<div id="crtbutton"><a href="mycart.php" id="cartorder">My cart</a>|<a href="#" onclick="deleteallcart()"id="cartdel">clear cart</a></div>' ;
			echo $display;
		}
		else{
			echo "You dont have any item in your cart";
		}
	}
	else{
		$display = '<table width="200" border="1">	<tr><th>Name</th><th>Price</th><th>Qty</th><th>x</th></tr>';
		$pid = $_SESSION['cart'][0]['productid'];
		$qty = $_SESSION['cart'][0]['qty'];
		$display .= displaycart($pid,$qty);
		$display .='</tr> </table>';
		
		echo $display;
	}
	
	
}else{
	echo "You dont have any item in your cart";

}

function displaycart($pid,$qty){
		$result=mysql_query("select * from tblswaping where id=$pid") or die("select name from products where id=$pid"."<br/><br/>".mysql_error());
		$display ="";
		$display .= "<tr>";
		while($row = mysql_fetch_array($result)){
			$display .=	'<td><label> '. $row['name'] .' </label></td>';
			$display .=	'<td> '. $row['price'] .' </td>  ';
			$display .=	'<td>'.$qty.'</td>';
			$display .=	'<td><a href="#" id="btndelete" onclick="deletecart('.$row['id'].')" >x</a></td>';
		}
		
		return $display	;
}



?>